let db = null;
function init(){
    const sqlite3 = require('sqlite3').verbose();

    db = new sqlite3.Database('./example.db', (err) => {
        if (err) {
            console.error(err.message);
        }
        console.log('Connected to the example database.');
    });

    db.run(`CREATE TABLE IF NOT EXISTS users (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      email TEXT NOT NULL UNIQUE
    )`, (err) => {
        if (err) {
            console.error(err.message);
        }
        console.log('Table created or already exists.');
    });
}
function insert(){
    let insertQuery = `INSERT INTO users(name, email) VALUES(?, ?)`;
    db.run(insertQuery, ["John Doe", "johndoe@example.com"], function(err) {
        if (err) {
            console.error(err.message);
        }
        console.log(`A new row has been inserted with rowid ${this.lastID}`);
    });
}

function select(callback){
    db.all(`SELECT * FROM users`, [], (err, rows) => {
        if (err) {
            console.error(err.message);
        }
        // rows.forEach((row) => {
        //     console.log(row);
        // });
        callback(rows);
    });
}


module.exports = { init, insert, select };